package appapi.service.impl;

import appapi.Request.AccessAuthPageReq;
import appapi.Request.bridge.AccessAuthVo;
import appapi.Request.bridge.AccessOperReq;
import appapi.Vo.brider.*;
import appapi.common.ResponseBean;
import appapi.dao.OperatorMapper;
import appapi.dao.StaffMapper;
import appapi.entity.basic.UserInfo;
import appapi.entity.bridge.AccessAuthInfo;
import appapi.mapper.AccessAuthMapper;
import appapi.mapper.AccessMapper;
import appapi.service.AccessAuthService;
import appapi.utils.BeanCopyUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hlm
 * @date
 * @description: 门禁人员
 */
@Service
public class AccessAuthServiceImpl extends ServiceImpl<AccessAuthMapper, AccessAuthInfo> implements AccessAuthService {

   @Resource
   private AccessAuthMapper accessAuthMapper;

   @Resource
   private AccessMapper accessMapper;

   @Resource
   private OperatorMapper operatorMapper;

   @Resource
   private StaffMapper staffMapper;

    /**
     * 分页查询门禁人员列表
     *
     * @return
     */
    @Override
    public IPage<AccessAuthVO> getAccessAuthByPage(AccessAuthPageReq req) {
        PageHelper.startPage(req.getPageNum(), req.getPageSize());
        List<AccessAuthVO>  accessAuthVOList = accessAuthMapper.getAccessAuthByPage(req);
        PageInfo<AccessAuthVO> info = new PageInfo<>(accessAuthVOList);
        IPage<AccessAuthVO> accessAuthVOIPage = new Page<>();
        if(accessAuthVOList.size() > 0){
            accessAuthVOIPage.setRecords(accessAuthVOList);
            accessAuthVOIPage.setSize(req.getPageSize());
            accessAuthVOIPage.setTotal(info.getTotal());
            accessAuthVOIPage.setCurrent(req.getPageNum());
        }
        return accessAuthVOIPage;
    }

    /**
     * 授权
     *
     * @return
     */
    @Override
    public void insertAccessAuth(AccessAuthVo authVo , UserInfo userInfo) {
        for (AddAccessAuth auth:authVo.getAuthList()
             ) {
            AccessAuthInfo accessAuthInfo = BeanCopyUtils.copyBean(auth ,AccessAuthInfo.class );
            accessAuthInfo.setUserId(auth.getAccessUserId());
            accessAuthInfo.setCreateTime(LocalDateTime.now());
            accessAuthInfo.setCreateUser(userInfo.getUserid());
            accessAuthInfo.setLastModifyUser(userInfo.getUserid());
            accessAuthInfo.setLastModifyTime(LocalDateTime.now());
            accessAuthInfo.setAccessId(auth.getAccessId());
            accessAuthInfo.setAuthFlag(1);
            //三年后过期
            accessAuthInfo.setExpirationTime(LocalDateTime.now().plusYears(3));
            accessAuthMapper.insert(accessAuthInfo);
        }
    }


    /**
     * 删除
     *
     * @return
     */
    @Override
    public void delAccessAuth(Long id) {
        AccessAuthInfo accessAuthInfo = accessAuthMapper.selectById(id);
        if(accessAuthInfo != null){
            accessAuthInfo.setAuthFlag(0);
            accessAuthMapper.updateById(accessAuthInfo);
        }
    }

    /**
     * 授权-待授权人员查询
     *
     * @return
     */
    @Override
    public ResponseBean<List<UserVO>> getOperator(AccessOperReq req){
        if(req.getUserType() == 0){
            //从操作员表中查
            return ResponseBean.success(operatorMapper.getOperator(req));
        }else {
            //从管理员表中查
            return ResponseBean.success(staffMapper.getOperator(req));
        }
    }

    /**
     * 授权-门禁查询
     *
     * @return
     */
    @Override
    public List<AccessVO> getAccess(Long projId){
        return accessMapper.selectAccessModel(projId);
    }
}
